package com.coocaa.ops.common.core.base.util;


import com.coocaa.ops.common.core.base.model.BaseModel;
import com.coocaa.ops.common.core.base.model.SimpleSysUserInfo;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/6/13.
 * @description 分页查询参数封装
 */
public class PageQueryUtil {

    /**
     * 封装查询参数
     *
     * @param model
     * @return
     */
    public static Map<String, Object> getFilterMap(BaseModel model) {
        Map<String, Object> params = new HashMap();
        InstanceUtil.mappingObject2Map(model, params);
        //进行查询参数设置
        handleFilterMap(model, params);
        return params;
    }


    /**
     * 进行查询参数设置
     *
     * @param model
     * @param params
     */
    public static void handleFilterMap(BaseModel model, Map<String, Object> params) {
        if(null != model.getStatus()){
            params.put("status", model.getStatus());
        }
        if (ArrayUtils.isNotEmpty(model.getStatusArray())) {
            params.put("statusArray", model.getStatusArray());
        }
        if(CollectionUtils.isNotEmpty(model.getBusinessTypeList())){
            params.put("businessTypeList", model.getBusinessTypeList());
        }
        //分页查询参数
        int offset = (model.getPage() - 1) * model.getRows();
        params.put("offset", offset);
        params.put("limit", model.getRows());
        if (StringUtils.isNotBlank(model.getKeyword())) {
            params.put("keyword", model.getKeyword());
        }
        SimpleSysUserInfo sysUserInfo = model.getUserInfo();
        if (null != sysUserInfo && null != sysUserInfo.getUserId()) {
            params.put("userId", sysUserInfo.getUserId());
        }
        if(null != model.getId()){
            params.put("id", model.getId());
        }
    }


}
